package thread;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

public class CallableThread implements Callable<Integer> {

    @Override
    public Integer call() throws Exception {
        int i = 0;
        for (i = 0; i < 100; i++) {
            System.out.println(Thread.currentThread().getName()+"  i:"+i);
        }
        return  i;
    }

    public static void main(String[] args){
        CallableThread callableThread = new CallableThread();
        FutureTask<Integer> task = new FutureTask<>(callableThread);
        for (int i = 0; i < 100; i++) {
            System.out.println(Thread.currentThread().getName()+" i:"+i);
            if (i==20){
                new Thread(task,"有返回值的线程").start();
            }
        }
        try {
            System.out.println("task get:"+task.get());
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
    }
}
